<template>
  <div class="app-container">

    <!--    搜索表单-->
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="客户端标识" prop="deptName">
        <el-input
          v-model="queryParams.clientId"
          placeholder="请输入客户端标识"
          clearable
          size="small"/>
      </el-form-item>
      <el-form-item label="授权类型" prop="status">
        <el-select v-model="queryParams.authorizedGrantTypes" placeholder="授权类型" clearable size="small">
          <el-option
            v-for="dict in authorizedGrantTypes"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!--    搜索表单-->

    <!--    功能列表-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
        >删除
        </el-button>
      </el-col>
    </el-row>
    <!--    功能列表-->

    <!--    数据-->
    <el-table v-loading="loading" :data="details">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="客户端标识" prop="clientId" width="120"/>
      <el-table-column label="客户端密钥" prop="clientSecret" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="授权范围" prop="scope" width="200"/>
      <el-table-column label="授权类型" prop="authorizedGrantTypes" width="200"/>
      <el-table-column label="回调地址" prop="webServerRedirectUri" width="200"/>
      <el-table-column label="access_token 过期时间(时)" prop="accessTokenValidity" width="200">
        <template slot-scope="scope">
          <span>{{ scope.row.accessTokenValidity / 3600 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="refresh_token 过期时间(天)" prop="refreshTokenValidity" width="200">
        <template slot-scope="scope">
          <span>{{ scope.row.refreshTokenValidity / 3600 / 24 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" width="180" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
    />
    <!--    数据-->

    <!--    弹出层-->
    <el-dialog top="5vh" :title="title" :visible.sync="open" width="620px" append-to-body>
      <el-form ref="form" label-position="top" :model="form" label-width="180px">

        <el-row :gutter="20" v-if="form.clientId != null">
          <el-col :span="12">
            <el-form-item label="客户端标识">
              <el-input v-model="form.clientId" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户端密钥">
              <el-input v-model="form.clientSecret" disabled/>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="权限范围(角色名称)">
              <el-select v-model="form.scope" multiple placeholder="请选择">
                <el-option
                  v-for="item in roleOptions"
                  :key="item.roleId"
                  :label="item.roleName"
                  :value="item.roleName"
                  :disabled="item.status == 0"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="授权类型">
              <el-select style="width: 100%" v-model="form.authorizedGrantTypes" multiple placeholder="请选择">
                <el-option
                  v-for="(item,index) in authorizedGrantTypes"
                  :key="item.dictLabel"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="回调地址">
              <el-input v-model="form.webServerRedirectUri"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="access_token 过期时间(时)" prop="dictSort">
              <el-input-number v-model="form.accessTokenValidity" controls-position="right" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="refresh_token 过期时间(天)" prop="dictSort">
              <el-input-number v-model="form.refreshTokenValidity" controls-position="right" :min="0"/>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--    弹出层-->


  </div>
</template>

<script>
  import {getDetailList, getClientDetail, addClient, updateClient, delClient} from "@/api/oauth"
  import {
    getBaseInfo
  } from "@/api/user"

  export default {
    name: 'Oauth',
    data() {
      return {
        title: "",
        open: false,
        form: {
          clientId: undefined,
          clientSecret: undefined,
          scope: [],
          authorizedGrantTypes: [],
          webServerRedirectUri: undefined,
          accessTokenValidity: undefined,
          refreshTokenValidity: undefined
        },
        // 总记录数
        total: 0,
        // 遮罩层
        loading: true,
        // 凭证数据
        details: [],
        // 非多个禁用
        multiple: true,
        // 授权类型
        authorizedGrantTypes: [],
        // 角色选项
        roleOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          clientId: undefined,
          authorizedGrantTypes: undefined
        },
      };
    },
    methods: {
      // 删除按钮操作
      handleDelete(row) {
        const clientId = row.clientId
        this.$confirm('是否确认删除"' + clientId + '"的客户端?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delClient(clientId)
        }).then(() => {
          this.getList()
          this.msgSuccess("删除成功")
        })
      },
      // 提交按钮
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.clientId != undefined) {
              updateClient(this.form).then(response => {
                this.msgSuccess("修改成功")
                this.open = false
                this.getList()
              })
            } else {
              addClient(this.form).then(response => {
                this.msgSuccess("新增成功")
                this.open = false
                this.getList()
              });
            }
          }
        });
      },
      // 修改按钮操作
      handleUpdate(row) {
        // 获取详情
        this.reset()
        const clientId = row.clientId
        getClientDetail(clientId).then(response => {
          this.form = response.data
          // 转换数据
          if (this.form.scope != null) {
            console.log(this.form.scope)
            this.form.scope = this.form.scope.split(",")
            console.log(this.form.scope)
          }
          if (this.form.authorizedGrantTypes != null) {
            this.form.authorizedGrantTypes = this.form.authorizedGrantTypes.split(",")
          }
          if (this.form.accessTokenValidity != null) {
            this.form.accessTokenValidity = this.form.accessTokenValidity / 3600
          }
          if (this.form.refreshTokenValidity != null) {
            this.form.refreshTokenValidity = this.form.refreshTokenValidity / 3600 / 24
          }
          this.open = true
          this.title = "修改凭证"

        });
      },
      // 新增按钮操作
      handleAdd() {
        this.reset()
        this.open = true
        this.title = "添加凭证"
      },
      // 重置按钮操作
      resetQuery() {
        this.resetForm("queryForm")
        this.handleQuery()
      },
      // 搜索按钮操作
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      // 获取客户端列表
      getList() {
        getDetailList(this.queryParams).then(res => {
          let data = res.data
          this.details = data.list
          this.total = data.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          clientId: undefined,
          clientSecret: undefined,
          scope: [],
          authorizedGrantTypes: [],
          webServerRedirectUri: undefined,
          accessTokenValidity: undefined,
          refreshTokenValidity: undefined
        }
        this.resetForm("form")
      },
    },
    created() {
      this.getList()
      this.getDicts("authorized_grant_types").then(response => {
        this.authorizedGrantTypes = response.data
      })
      getBaseInfo().then(res => {
        let data = res.data
        this.roleOptions = data.roles
      })
    },
    components: {}
  }
</script>

<style scoped lang='scss'>
  ::v-deep .el-dialog__body {
    padding: 10px 30px 0 30px;
  }
</style>
